package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.common.DatabaseManager;
import com.aduerr.v1shouyin.entity.MemberInfoEntity;
import com.aduerr.v1shouyin.entity.MemberRechargeAndConsumeEntity;
import com.aduerr.v1shouyin.entity.MemberToPayEntity;
import com.aduerr.v1shouyin.entity.OrderGetPosOrderLEntity;
import com.aduerr.v1shouyin.entity.OrderRecordEntity;
import com.aduerr.v1shouyin.entity.PayStateEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.LogUtils;
import com.aduerr.v1shouyin.util.MessageEvent;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ResultActivity_bak extends BaseUnionActivity {

    public static final String TAG = "ResultActivity";

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_sucussorfail_state)
    ImageView ivSucussorfailState;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.rl_time)
    RelativeLayout accounttime;

    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_timecontetn)
    TextView tv_timecontetn;
    @BindView(R.id.tv_confrom)
    TextView tvConfrom;

    @BindView(R.id.ll_wx_pay)
    LinearLayout llWxPay;
    @BindView(R.id.tv_order_money)
    TextView tvOrderMoney;
    @BindView(R.id.tv_sell_cut)
    TextView tvSellCut;
    @BindView(R.id.tv_real_money)
    TextView tvRealMoney;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;

    @BindView(R.id.ll_vip_pay)
    LinearLayout llVipPay;
    @BindView(R.id.tv_vip_name)
    TextView tvVipName;
    @BindView(R.id.tv_vip_cut)
    TextView tvVipCut;
    @BindView(R.id.tv_vip_banlance)
    TextView tvVipBanlance;
    @BindView(R.id.tv_vip_add)
    TextView tvVipAdd;
    @BindView(R.id.tv_vip_integral)
    TextView tvVipIntegral;
    @BindView(R.id.rl_vip_discount)
    RelativeLayout rlVipDiscount;


    private String type;

    private String[] content = new String[20];
    private String beizhu;
    private MemberToPayEntity.DataBean memberInfo;
    private PayStateEntity payStateEntity;
    private String sendmoney;

    //倒计时;
    private int recLen = 3;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch(msg.what) {
                //显示进度条
                case 1:
                    recLen--;
                    tvTime.setText("" + recLen);
                    if(recLen > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);      // send message
                    } else {
                        //交易成功了才发送消息
                        if(type != null) {
                            /*if (type.equals("1") | type.equals("6") | type.equals
                            ("8") | type.equals("10")) {
                                Intent intent = new Intent(ResultActivity.this,
                                CalculatorActivity.class);
                                startActivity(intent);*/
                            EventBus.getDefault().postSticky(new MessageEvent("sucuss"));
//                            }
                        }
                        finish();
                    }
                    break;
            }
            return false;
        }
    });

    @Override
    protected int getLayoutId() {
        return R.layout.activity_result;
    }

    @Override
    protected void initBase() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("订单详情");
        initDate();
    }


    private void initDate() {
        beizhu = getIntent().getStringExtra("beizhu");
        LogUtils.e("结果界面的备注", "备注" + beizhu);
        String money = getIntent().getStringExtra("money");
        //有关扫码成功后返回的javabean
        payStateEntity =
                (PayStateEntity) getIntent().getSerializableExtra(Constants.PAYSTATEENTITY);

        type = getIntent().getStringExtra("sucess");
        sendmoney = getIntent().getStringExtra("sendmoney");

        //当前的时间
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String time = sdFormatter.format(curDate);
        //当前的时间

        if(type != null) {
            if(type.equals("0")) {//失败
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_fail);
                tvState.setText("收款失败");
                tvTitle.setText("收款结果");
                tvConfrom.setText("重新收款");
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.GONE);
                beginCountDown();
            } else if(type.equals("1")) {//现金成功
                getOrderDetail(getIntent().getStringExtra("orderid"));
                llWxPay.setVisibility(View.VISIBLE);
                llVipPay.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_sucuss);
                tvState.setText("收款成功");
                tvTitle.setText("收款结果");
                tvConfrom.setText("完成");
                String orderid = getIntent().getStringExtra("orderid");
                String zhaoling = getIntent().getStringExtra("zhaoling");
                String shishou = getIntent().getStringExtra("shishou");
                content[0] = "现金收款";
                content[1] = orderid;
                content[3] = money;
                content[4] = Hawk.get("company");
                content[5] = Hawk.get("sellername");
                content[6] = Hawk.get("shopname");
                content[7] = time;
                content[8] = beizhu;
                OrderRecordEntity order = new OrderRecordEntity();
                order.setType(13);
                order.setOrderId(content[1]);
                order.setOrderMoney(content[3]);
                order.setMerchantName(content[4]);
                order.setCashier(content[5]);
                order.setShopName(content[6]);
                order.setPayTime(content[7]);
                order.setRemarks(content[8]);
                DatabaseManager.getInstance().insert(order);
                print();
            } else if(type.equals("3")) {//成功
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.VISIBLE);
                rlVipDiscount.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_sucuss);
                tvState.setText("会员充值成功");
                tvTitle.setText("充值结果");
                tvConfrom.setText("完成");
                //会员扫码充值
                String orderid = getIntent().getStringExtra("orderid");
                String scanMemberNo = getIntent().getStringExtra("scanMemberNo");
//                if (!TextUtils.isEmpty(scanMemberNo)) {
//                    toScanMemberInfor(scanMemberNo);
//                }
                //如果是现金付款
                String paytype = getIntent().getStringExtra("paytype");
                if(!TextUtils.isEmpty(paytype)) {
                    content[8] = "现金付款";
                }

                MemberInfoEntity.DataBean befordatebean =
                        (MemberInfoEntity.DataBean) getIntent().getSerializableExtra(
                                "beformemberincahrgeinfo");
                tvVipName.setText(befordatebean.getNames());
                tvVipCut.setText(befordatebean.getDiscount());
                tvVipBanlance.setText(befordatebean.getBalance());
                tvVipIntegral.setText(befordatebean.getIntegral());
                tvVipAdd.setText(befordatebean.getChangeint() + "");
                if(payStateEntity != null) {
                    PayStateEntity.DataBean data = payStateEntity.getData();
                    if(data != null) {
                        int payType = data.getPayType();
                        if(payType == 0) {
                            content[8] = "微信收款";
                        } else if(payType == 1) {
                            content[8] = "支付宝收款";
                        } else if(payType == -1) {
                            content[8] = "其他方式收款";
                        }
                    }
                }
                content[1] = "会员充值";
                content[2] = money + "";//商家实收
                if(befordatebean != null) {
                    content[3] = befordatebean.getNames();//会员姓名
                } else {
                    content[3] = " ";//会员姓名
                }
                if(befordatebean != null) {
                    String phone = befordatebean.getPhone();
//                    boolean ismobileNum = phoneNumberutil.isMobileNum(phone);
                    if(phone != null && phone.length() == 11) {
                        String maskNumber =
                                phone.substring(0, 3) + "****" + phone.substring(7,
                                        phone.length());
                        content[4] = maskNumber;//电话号码
                    } else {
                        content[4] = "";
                    }
                }
                if(befordatebean != null) {
                    content[5] = befordatebean.getDiscount() + " ";
                    content[6] = befordatebean.getLevelName() + " ";
                    content[7] = befordatebean.getIntegral() + " ";
                } else {
                    content[5] = " ";
                    content[6] = " ";
                    content[7] = " ";
                }
                content[9] = Hawk.get("sellername");
                content[0] = Hawk.get("shopname");
                content[10] = Hawk.get("company");
                content[11] = time;
                content[12] = beizhu + " ";
                //会员现金充值
                MemberRechargeAndConsumeEntity.DataBean afterdataBean =
                        (MemberRechargeAndConsumeEntity.DataBean) getIntent().getSerializableExtra("aftermemberincahrgeinfo");
                if(afterdataBean != null) {
                    content[13] = afterdataBean.getBalance() + " ";
                } else {
                    if(befordatebean != null) {
                        content[13] = befordatebean.getBalance() + " ";
                    } else {
                        content[13] = " ";
                    }
                }
                //现金没有生成订单所以判断是否生成了orderid
                if(!TextUtils.isEmpty(orderid)) {
                    content[14] = orderid + " ";
                }
                if(sendmoney == null) {
                    content[15] = " ";//赠送金额

                } else {
                    content[15] = sendmoney + " ";//赠送金额
                }

                OrderRecordEntity order = new OrderRecordEntity();
                order.setType(3);
                order.setOrderId(content[14]);
                order.setMerchantName(content[10]);
                order.setRechargeType(content[8]);
                order.setOrderMoney(content[2]);
                order.setSendMoney(content[15]);
                order.setBalance(content[13]);
                order.setCustomer(content[3]);
                order.setPhone(content[4]);
                order.setDiscount(content[5]);
                order.setIntegral(content[7]);
                order.setPayTime(content[11]);
                order.setShopName(content[0]);
                order.setCashier(content[9]);
                order.setRemarks(content[12]);
                DatabaseManager.getInstance().insert(order);
                print();

            } else if(type.equals("4")) {//失败
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_fail);
                tvState.setText("会员充值失败");
                tvTitle.setText("充值结果");
                tvConfrom.setText("重新收款");
                beginCountDown();
            } else if(type.equals("5")) {//失败
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_fail);
                tvState.setText("会员消费失败");
                tvTitle.setText("充值结果");
                tvConfrom.setText("重新收款");
                beginCountDown();
            } else if(type.equals("6")) {
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.VISIBLE);
                rlVipDiscount.setVisibility(View.VISIBLE);
                //会员消费成功
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_sucuss);
                tvState.setText("会员消费成功");
                tvTitle.setText("消费结果");
                tvConfrom.setText("完成");
                String scanMemberNo = getIntent().getStringExtra("scanMemberNo");
//                if (!TextUtils.isEmpty(scanMemberNo)) {
//                    toScanMemberInfor(scanMemberNo);
//                }
                if(money != null) {
                    tvMoney.setText(money + "");
                }

                memberInfo =
                        (MemberToPayEntity.DataBean) getIntent().getSerializableExtra(
                                "memberInfo");

                tvVipName.setText(memberInfo.getName());
                tvVipCut.setText(memberInfo.getDiscount());
                tvVipBanlance.setText(memberInfo.getBalance());
                tvVipIntegral.setText(memberInfo.getIntegral() + "");
                tvVipAdd.setText(memberInfo.getChangeint() + "");

                content[0] = "会员消费";
                content[1] = money + "";
                content[2] = memberInfo.getName();
                if(!TextUtils.isEmpty(memberInfo.getPhone())) {
                    String phone = memberInfo.getPhone();
                    String maskNumber =
                            phone.substring(0, 3) + "****" + phone.substring(7,
                                    phone.length());
                    content[3] = maskNumber;
                } else {
                    content[3] = "****";
                }
                content[4] = memberInfo.getDiscount();
                content[5] = memberInfo.getName();
                content[6] = memberInfo.getIntegral() + "";
                content[7] = "会员";
                content[8] = Hawk.get("sellername");
                content[9] = Hawk.get("shopname");
                content[10] = Hawk.get("company");
                content[11] = time;
                content[12] = " " + beizhu;

                MemberToPayEntity memberToPayEntity =
                        (MemberToPayEntity) getIntent().getSerializableExtra("datebeen");
                String orderid = memberToPayEntity.getData().getOrderid();
                LogUtils.e("订单号", "*****************************" + orderid);
                if(!TextUtils.isEmpty(orderid)) {
                    content[14] = orderid;
                } else {
                    content[14] = "";
                }
                content[13] = memberToPayEntity.getData().getBalance();//会员余额
                OrderRecordEntity order = new OrderRecordEntity();
                order.setType(4);
                order.setOrderId(content[14]);
                order.setMerchantName(content[10]);
                order.setOrderMoney(content[1]);
                order.setBalance(content[13]);
                order.setCustomer(content[5]);
                order.setPhone(content[3]);
                order.setIntegral(content[6]);
                order.setPayTime(content[11]);
                order.setShopName(content[9]);
                order.setCashier(content[8]);
                order.setRemarks(content[12]);
                DatabaseManager.getInstance().insert(order);
                print();

            } else if(type.equals("7")) {//扫码收款失败
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_fail);
                tvState.setText("消费失败");
                tvTitle.setText("收款结果");
                if(money != null) {
                    tvMoney.setText(money + "");
                }
                tvConfrom.setText("重新收款");
                beginCountDown();
            } else if(type.equals("8")) {
                getOrderDetail(getIntent().getStringExtra("orderid"));
                llWxPay.setVisibility(View.VISIBLE);
                llVipPay.setVisibility(View.GONE);
                //扫码收款成功
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_sucuss);
                tvState.setText("消费成功");
                tvTitle.setText("收款结果");
                tvConfrom.setText("完成");
                if(money != null) {
                    tvMoney.setText(money + "");
                }
                String orderid = getIntent().getStringExtra("orderid");
                content[0] = "扫码收款";
                content[1] = orderid;
                content[2] = money;
                content[3] = "0.00";
                content[4] = Hawk.get("sellername");
                content[5] = Hawk.get("shopname");
                content[10] = Hawk.get("company");
                content[6] = time;
                content[7] = beizhu;
                //有关支付方式的打印的样式
                OrderRecordEntity order = new OrderRecordEntity();
                if(payStateEntity != null) {
                    PayStateEntity.DataBean data = payStateEntity.getData();
                    if(data != null) {
                        content[9] = data.getBuyerUser();
                        int payType = data.getPayType();
                        if(payType == 0) {
                            content[8] = "微信收款";
                            order.setType(2);
                        } else if(payType == 1) {
                            content[8] = "支付宝收款";
                            order.setType(1);
                        } else if(payType == -1) {
                            content[8] = "其他方式收款";
                            order.setType(0);
                        }
                    }
                    order.setOrderId(orderid);
                    order.setMerchantName(Hawk.get("company"));
                    order.setOrderState("已付款");
                    order.setOrderMoney(money);
                    order.setPayTime(time);
                    order.setShopName(Hawk.get("shopname"));
                    order.setCashier(Hawk.get("sellername"));
                    order.setCustomer(data.getBuyerUser());
                    order.setRemarks(beizhu);
                    DatabaseManager.getInstance().insert(order);
                    print();
                }

            } else if(type.equals("9")) {//扫描生成的二维码,付款失败
                llWxPay.setVisibility(View.GONE);
                llVipPay.setVisibility(View.GONE);
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_fail);
                tvState.setText("收款失败");
                tvTitle.setText("收款结果");
                if(money != null) {
                    tvMoney.setText(money + "");
                }
                tvConfrom.setText("重新收款");
                beginCountDown();
            } else if(type.equals("10")) {
                getOrderDetail(getIntent().getStringExtra("orderid"));
                llWxPay.setVisibility(View.VISIBLE);
                llVipPay.setVisibility(View.GONE);
                //扫描生成的二维码,付款成功
                ivSucussorfailState.setImageResource(R.drawable.img_dailog_sucuss);
                tvState.setText("收款成功");
                tvTitle.setText("收款结果");
                tvConfrom.setText("完成");
                if(money != null) {
                    tvMoney.setText(money + "");
                }
                String orderid = getIntent().getStringExtra("orderid");
                content[0] = "扫码收款";
                content[1] = orderid;
                content[2] = money;
                content[3] = "0.00";
                content[4] = Hawk.get("sellername");
                content[5] = Hawk.get("shopname");
                content[10] = Hawk.get("company");
                content[6] = time;
                content[7] = beizhu;

                OrderRecordEntity order = new OrderRecordEntity();
                //有关支付方式的打印的样式
                if(payStateEntity != null) {
                    PayStateEntity.DataBean data = payStateEntity.getData();
                    if(data != null) {
                        content[9] = data.getBuyerUser();
                        int payType = data.getPayType();
                        if(payType == 0) {
                            content[8] = "微信收款";
                            order.setType(2);
                        } else if(payType == 1) {
                            content[8] = "支付宝收款";
                            order.setType(1);
                        } else if(payType == -1) {
                            content[8] = "其他方式收款";
                            order.setType(0);
                        }
                    }
                    order.setOrderId(orderid);
                    order.setMerchantName(Hawk.get("company"));
                    order.setOrderState("已付款");
                    order.setOrderMoney(money);
                    order.setPayTime(time);
                    order.setShopName(Hawk.get("shopname"));
                    order.setCashier(Hawk.get("sellername"));
                    order.setCustomer(data.getBuyerUser());
                    order.setRemarks(beizhu);
                    DatabaseManager.getInstance().insert(order);

                    Boolean b =
                            (Boolean) SharedPreferencesUtils.getParam(ResultActivity_bak.this, "autoprint", false);

                    if(b) return;

                    print();
                }
            }

            //收款的钱数
            if(money != null) {
                tvMoney.setText(money + "");
            }
        }
    }

    void print() {
        int printpage = (int) SharedPreferencesUtils.getParam(ResultActivity_bak.this,
                "printpage", 1);
        if(printpage == 0) {
            ToastUtils.showToast(ResultActivity_bak.this, "如果想自动打印小票,请到设置界面设置打印张数!!!");
            return;
        }

        if(content != null) {
            if(type.equals("1")) {
                print(null, content, Constants.PRINT_CASH_ORDER, printpage);
            } else if(type.equals("3")) {
                print(null, content, Constants.PRINT_MEMBER_RECHARGE_2, printpage);
            } else if(type.equals("6")) {
                print(null, content, Constants.PRINT_MEMBER_CONSUME_RESULT, printpage);
            } else if(type.equals("8")) {
                if(payStateEntity != null) {
                    int payType = payStateEntity.getData().getPayType();
                    Bitmap bitmap = null;
                    if(payType == 0) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_w);
                    } else if(payType == 1) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_z);
                    } else {//其他
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_public);
                    }
                    print(bitmap, content, Constants.PRINT_SCAN_ORDER, printpage);
                }
            } else if(type.equals("10")) {
                if(payStateEntity != null) {
                    int payType = payStateEntity.getData().getPayType();
                    Bitmap bitmap = null;
                    if(payType == 0) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_w);
                    } else if(payType == 1) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_z);
                    } else {//其他
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_public);
                    }
                    print(bitmap, content, Constants.PRINT_SCAN_ORDER, printpage);
                }
            }
        }
    }

    private void print(Bitmap logo, String[] content, int orderType, int printPage) {

        printOrder(logo, content, orderType, printPage);
    }

    private void printOrder(Bitmap logo, String[] content, int orderType, int printPage) {
        PrinterWrapper.printPosOrder(logo, content, orderType, printPage);
    }


    @OnClick({R.id.img_back, R.id.tv_confrom})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
            case R.id.tv_confrom:
                if(recLen > 0) {
//                    finish();
                }
                if(type != null) {
                    EventBus.getDefault().postSticky(new MessageEvent("sucuss"));
//                    }
                }
                finish();
                break;
        }


    }


    private void getOrderDetail(String orderId) {

        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("orderid", orderId);
//        getView().showProgress();
        APIRetrofit.getAPIService().orderGetPosOrderL(RXRequest.getXingPosParams(addParams, this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderGetPosOrderLEntity>() {


                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull OrderGetPosOrderLEntity orderGetPosOrderLEntity) {
                        int success = orderGetPosOrderLEntity.getSuccess();
                        if(success == 1) {
                            OrderGetPosOrderLEntity.DataBean.ListBean orderDetail =
                                    orderGetPosOrderLEntity.getData().get(0).getList().get(0);
                            tvOrderMoney.setText(orderDetail.getOrdermoney());
                            tvSellCut.setText(orderDetail.getSelleryouhui());
                            tvRealMoney.setText(orderDetail.getRealpay());
                            tvOrderType.setText(orderDetail.getPaytype());
                            tvOrderId.setText(orderDetail.getOrderid());
                            tvOrderTime.setText(orderDetail.getDate());

                            printOrder(orderGetPosOrderLEntity);

                        } else if(success == -100 | success == -200) {
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            SharedPreferencesUtils.remove(ResultActivity_bak.this,
                                    "autologin");
                            SharedPreferencesUtils.remove(ResultActivity_bak.this,
                                    "remberpassword");
                            startActivity(new Intent(ResultActivity_bak.this,
                                    BangGuidActivity.class));
                            finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(orderGetPosOrderLEntity.getErrMsg());
                            SharedPreferencesUtils.remove(ResultActivity_bak.this,
                                    "autologin");
                            SharedPreferencesUtils.remove(ResultActivity_bak.this,
                                    "remberpassword");
                            //// TODO: 2017/12/9 0009 去登录的界面
                            startActivity(new Intent(ResultActivity_bak.this,
                                    LoginActivity.class));
                            finish();
                        } else {
                            ToastUtils.showToast(ResultActivity_bak.this,
                                    orderGetPosOrderLEntity.getErrMsg() + "");
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(ResultActivity_bak.this, "网络连接超时",
                                    Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(ResultActivity_bak.this, "连接失败",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(ResultActivity_bak.this, "检查网络状态",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    //打印完成的回调方法
    public void beginCountDown() {
//        tvConfrom.setVisibility(View.INVISIBLE);
        tv_timecontetn.setVisibility(View.VISIBLE);
        tvTime.setVisibility(View.VISIBLE);
        Message message = handler.obtainMessage(1);     // Message
        handler.sendMessageDelayed(message, 1000);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        content = null;
        //解绑服务  重要的异常信息
        // Activity has leaked ServiceConnection com.aduer.shouyin.mvp.bleprint
        // .XingPosPrintUtil$1@14c577e8 that was originally bound here
    }

    //物理返回键
    @Override
    public void onBackPressed() {
    }


    /**
     * 小票新样式打印方法
     * 时间：202007
     *
     * @param entity
     */
    public void printOrder(OrderGetPosOrderLEntity entity) {
        int printPage = (int) SharedPreferencesUtils.getParam(ResultActivity_bak.this,
                "printpage", 1);
        if(printPage == 0) {
            ToastUtils.showToast(ResultActivity_bak.this, "如果想自动打印小票,请到设置界面设置打印张数!!!");
            return;
        }
        int orderType=0;
        if(type.equals("8")){
            orderType = Constants.PRINT_SCAN_ORDER;
        }

        // 门店 收银员 支付方式  支付状态  支付时间  备注  订单金额 实收金额
        try {
            content[0] = entity.getData().get(0).getList().get(0).getOrderid();
            content[1] = entity.getData().get(0).getList().get(0).getSiteusername();
            content[2] = entity.getData().get(0).getList().get(0).getPaytype();
            content[3] = entity.getData().get(0).getList().get(0).getPaystate();
            content[4] = entity.getData().get(0).getList().get(0).getPaytime();
            content[5] = entity.getData().get(0).getList().get(0).getOrdernote();
            content[6] = entity.getData().get(0).getList().get(0).getOrdermoney();
            content[7] = entity.getData().get(0).getList().get(0).getRealpay();
            PrinterWrapper.printPosOrder(null, content, orderType, printPage);

        }catch(Exception e){
            Log.i("ResultActivity",e.getMessage()+"");
        }

    }

}
